/**
 * 应用商店管理
 */
$(document).ready(function() {
	var appStoreDialog;
	var bindModelData = {
		appStoreForm:{appGoodsId:"",price:"",unitClassify:"",unitType:"",validityClassify:"",category:""},
		appStoreSearch:{name:""}
	}
	var appStoreFormModel,appStoreSearchFormModel,appStoreFormValidator;
	function init(){
		initSearchForm();
		initAppStoreForm();
		if($("#search").data("LBSFormSearch")===undefined){
			$("#search").LBSFormSearch({
				searchBar:true,
				searchHandle:searchHandle,
				resetHandle:resetSearchHandle,
				custom:[{name:"add",value:"添加",click:function(e){addAppStoreHandle(e);},spriteCssClass:"k-icon k-i-plus"}]
			});
		}
		
		if($("#app_store_grid").data("kendoGrid")===undefined){
			$("#app_store_grid").kendoGrid({
				autoBind:true,
		        dataSource: getGridDataSource({
		        	read:{
		        		url:"/system/appStore/find",
		        	},
		        	model:{
		        		id:"id",
		        		fields:{
		        			id:{type:"string"},
		        			appGoodsName:{type:"string"},
		        			createTime:{type:"date"},
		        			status:{type:"string"}
		        		}
		        	}
		        }),
		        sortable: true,
		        selectable: true,
		        editable: false,
		        resizable: true,
		        pageable: true,
		        dataBound:function(e){
		        	var grid = e.sender;
		        	var dataItems = grid.dataSource.view();
		        	for (var j = 0; j < dataItems.length; j++) {
		        		var row = grid.tbody.find("[data-uid='" + dataItems[j].uid + "']");
		        		console.log(row);
		        		var modifyButton = $(row).find("a.k-grid-modify");
						var removeButton = $(row).find("a.k-grid-down");
						var addButton = $(row).find("a.k-grid-put");
						var status = dataItems[j].status;
						if(status=="初始"||status=="下架"){
							removeButton.hide();
						}else{
							addButton.hide();
						}
		        	}
		        },
		        dataBinding: function() {
		            record = (this.dataSource.page() -1) * this.dataSource.pageSize();
		        },
		        columns: [
		            {title: "序号",template: "#= ++record #",width: 50},
		            {field: "id",title: "编号",hidden:true}, 
		            {field: "category",title: "品类名称"}, 
		            {field: "appGoodsName",title: "应用名称"}, 
		            {field: "status",title: "状态",values:getStaticData("app_store_status")}, 
		            {field: "validityClassify",title: "是否限制",values:getStaticData("app_vilidaty_classify")}, 
		            {field: "createTime",title: "创建时间",format: "{0: yyyy-MM-dd HH:mm:ss}"},
		            {title: "操作" ,command: [{name: "modify",text:"编辑", click:modifyAppStoreHandle},
		                                    {name: "put",text:"上架", click:putAppStoreHandle},
		                                    {name: "down",text:"下架", click:downAppStoreHandle}],width: 160}
		        ]
		    });
		}
	}
	function initSearchForm(){
		if($("input[name='name']",$("#search")).data("kendoMaskedTextBox")===undefined){
			$("input[name='name']",$("#search")).kendoMaskedTextBox();
		}
		appStoreSearchFormModel = kendo.observable(bindModelData.appStoreSearch);
		kendo.bind("#search", appStoreSearchFormModel);
	}
	function initAppStoreForm(){
		appStoreFormValidator = $("#app_store_form").kendoValidator().data("kendoValidator");
		if($("#app_store_form").data("LBSDialog")===undefined){
			appStoreDialog = $("#app_store_form").LBSDialog({
				height:250,
				width:550,
				deactivate:resetFormHandle,
				bbars:{
					save:saveFormHandle
				}
			});
		}
		
		if ($("input[name='appGoods']", $("#app_store_form")).data("kendoComboBox") === undefined) {
			$("input[name='appGoods']", $("#app_store_form")).kendoComboBox({
				valuePrimitive : true,
				dataTextField : "name",
				dataValueField : "id",
				dataSource : getListDataSource({
					url : "/system/appGoods/dataList"
				}),
				placeholder : "请选择应用菜单...",
			});
		}
		if($("input[name='category']",$("#app_store_form")).data("kendoMaskedTextBox")===undefined){
			$("input[name='category']",$("#app_store_form")).kendoMaskedTextBox();
		}
		if ($("input[name='validityClassify']", $("#app_store_form")).data("kendoComboBox") === undefined) {
			$("input[name='validityClassify']", $("#app_store_form")).kendoComboBox({
				valuePrimitive : true,
				dataTextField : "name",
				dataValueField : "id",
				dataSource: getListDataSource({url:"/common/dict/app_vilidaty_classify/data"}),
				placeholder : "请选择有效分类...",
				change:function(e){
					//获取选择类型
					var index = this.select();
					var dataItem = this.dataItem(index);
					var unitClassify = $("input[name='unitClassify']", $("#app_store_form")).data("kendoComboBox");
					var unitType = $("input[name='unitType']", $("#app_store_form")).data("kendoComboBox");
					debugger;
					if(dataItem.code == "LIMIT"){
						unitClassify.enable(true);
						unitType.enable(true); 
					}
					if(dataItem.code == "UNLIMIT"){
						unitClassify.enable(false);
						unitType.enable(false); 
					}
				}
			});
		}
		if ($("input[name='unitType']", $("#app_store_form")).data("kendoComboBox") === undefined) {
			$("input[name='unitType']", $("#app_store_form")).kendoComboBox({
				enable:false,
				valuePrimitive : true,
				dataTextField : "name",
				dataValueField : "id",
				dataSource : [],
				placeholder : "请选择计量单位...",
			});
		}
		if ($("input[name='unitClassify']", $("#app_store_form")).data("kendoComboBox") === undefined) {
			$("input[name='unitClassify']", $("#app_store_form")).kendoComboBox({
				enable:false,
				valuePrimitive : true,
				dataTextField : "name",
				dataValueField : "id",
				dataSource : getListDataSource({
					url : "/common/unitClassify/dataList"
				}),
				change:function(e){
					var unitClassifyId = this.value();
					var dataSource = getListDataSource({url : "/common/unitType/dataList",params:{unitClassifyId:unitClassifyId}});
					var unitType = $("input[name='unitType']", $("#app_store_form")).data("kendoComboBox");
					unitType.setDataSource(dataSource);
				},
				placeholder : "请选择计量分类...",
			});
		}
		if($("input[name='price']",$("#app_store_form")).data("kendoMaskedTextBox")===undefined){
			$("input[name='price']",$("#app_store_form")).kendoMaskedTextBox();
		}
	}
	function main(){
		init();
	}
	function searchHandle(){
		$("#app_store_grid").data("kendoGrid").dataSource.read({content:kendo.stringify(appStoreSearchFormModel)});
	}
	function resetSearchHandle(){
		appStoreSearchFormModel = kendo.observable(bindModelData.appStoreSearch);
		kendo.bind("#search", appStoreSearchFormModel);
	}
	function resetFormHandle(){
		$("input[name='appGoods']", $("#app_store_form")).data("kendoComboBox").enable(true);
		appStoreFormModel = kendo.observable(bindModelData.appStoreForm);
		kendo.bind("#app_store_form", appStoreFormModel);
	}
	function saveFormHandle(){
		var url = "/system/appStore/save";
		if(this.paramters!==undefined){url = this.paramters.url}
		if(appStoreFormValidator.validate()){
			httpRequestHandle({
				url:url,
				data:{content:kendo.stringify(appStoreFormModel)}
			}).done(function(response){
				$("#app_store_grid").data("kendoGrid").dataSource.read();
			});
			this.close();
		}else{
			LBSNotification({message:"检查表格字段是否为空"});
		}
		delete this.paramters;
	}
	function addAppStoreHandle(){
		appStoreFormModel = kendo.observable(bindModelData.appStoreForm);
		kendo.bind("#app_store_form", appStoreFormModel);
		appStoreDialog.open();
	}
	function modifyAppStoreHandle(e){
		$("input[name='appGoods']", $("#app_store_form")).data("kendoComboBox").enable(false);
		var grid = $("#app_store_grid").data("kendoGrid");
		var tr = $(e.target).closest("tr");
		var data = grid.dataItem(tr);
		var validityClassifyData = $("input[name='validityClassify']", $("#app_store_form")).data("kendoComboBox").dataSource.data();
		var validityClassifyCode = "";
		for(var x in validityClassifyData){
			var validityClassifyId = validityClassifyData[x].id;
			if(validityClassifyId===data.validityClassify){
				validityClassifyCode = validityClassifyData[x].code;
			}
		}
		if(validityClassifyCode == "LIMIT"){
			$("input[name='unitType']", $("#app_store_form")).data("kendoComboBox").enable(true); 
			$("input[name='unitClassify']", $("#app_store_form")).data("kendoComboBox").enable(true);
		}
		if(validityClassifyCode == "UNLIMIT"){
			$("input[name='unitType']", $("#app_store_form")).data("kendoComboBox").enable(false); 
			$("input[name='unitClassify']", $("#app_store_form")).data("kendoComboBox").enable(false);
		}
		var dataSource = getListDataSource({url : "/common/unitType/dataList",params:{unitClassifyId:data.unitClassify}});
		var unitType = $("input[name='unitType']", $("#app_store_form")).data("kendoComboBox");
		unitType.setDataSource(dataSource);
		appStoreFormModel = kendo.observable(data);
		kendo.bind("#app_store_form", appStoreFormModel);
		appStoreDialog.open({paramters:{url:"/system/appStore/update"}});
	}
	function putAppStoreHandle(e){
		var grid = $("#app_store_grid").data("kendoGrid");
		var tr = $(e.target).closest("tr");
		var data = grid.dataItem(tr);
		httpRequestHandle({
			url:"/system/appStore/put",
			data:{id:data.id}
		}).done(function(response){
			$("#app_store_grid").data("kendoGrid").dataSource.read();
		});
	}
	function downAppStoreHandle(e){
		var grid = $("#app_store_grid").data("kendoGrid");
		var tr = $(e.target).closest("tr");
		var data = grid.dataItem(tr);
		httpRequestHandle({
			url:"/system/appStore/down",
			data:{id:data.id}
		}).done(function(response){
			$("#app_store_grid").data("kendoGrid").dataSource.read();
		});
	}
	main();
});